﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HL.Core.Models;
using HL.Lib.CPControllers;
using HL.Lib.MVC;

namespace HL.Lib.Models
{
    public class MMapLocationEntity : EntityBase
    {
        [DataInfo]
        public int MapId { get; set; }

        [DataInfo]
        public int LangID { get; set; }

        [DataInfo]
        public int Catalogue { get; set; }

        [DataInfo]
        public int City { get; set; }

        [DataInfo]
        public int Price { get; set; }

        [DataInfo]
        public String Longtitude { get; set; }

        [DataInfo]
        public String Lattitude { get; set; }

        [DataInfo]
        public String Tittle { get; set; }

        [DataInfo]
        public String Content { get; set; }

        [DataInfo]
        public String Intro { get; set; }

        [DataInfo]
        public String ImgUrl { get; set; }

        [DataInfo]
        public int DeleteFlg { get; set; }


        [DataInfo]
        public String Address { get; set; }

        [DataInfo]
        public decimal PriceValue { get; set; }

        [DataInfo]
        public override int ID { get; set; }

        private MMapCityEntity cityEntity;

        public MMapCityEntity getCityEntity()
        {
            if (cityEntity != null && City > 0)
                cityEntity = MMapCityService.Instance.GetByID(City);

            if (cityEntity == null)
                cityEntity = new MMapCityEntity();

            return cityEntity;
        }

        private MMapPriceEntity _priceEntity;

        public MMapPriceEntity getPriceEntity()
        {
            if (Price != null && Price > 0)
                _priceEntity = MMapPriceService.Instance.GetById(Price);

            if (cityEntity == null)
                _priceEntity = new MMapPriceEntity();

            return _priceEntity;
        }

        private MMapCatalogueEntity catalogueEntity;

        public MMapCatalogueEntity getCatalogueEntity()
        {
            if (Catalogue != null && Catalogue > 0)
                catalogueEntity = MMapCatalogueService.Instance.GetById(Catalogue);

            if (cityEntity == null)
                catalogueEntity = new MMapCatalogueEntity();

            return catalogueEntity;
        }
    }

    public class MMapLocationService : ServiceBase<MMapLocationEntity>
    {
        #region Autogen by HL

        public MMapLocationService()
            : base("[MAP_WEDDING_LOCATION]")
        {
        }

        private static MMapLocationService _instance = null;

        public static MMapLocationService Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new MMapLocationService();

                return _instance;
            }
        }

        #endregion

        public MMapLocationEntity GetByID(int id)
        {
            return base.CreateQuery()
                .Where(o => o.ID == id)
                .ToSingle();
        }

        public List<MMapLocationEntity> GetList()
        {
            return base.CreateQuery()
                .Where(o => o.DeleteFlg == 0).ToList();
        }

        public int GetMaxId()
        {
            return base.CreateQuery()
                       .Max(o => o.MapId)
                       .ToValue().ToInt(0) + 1;
        }

        public List<MMapLocationEntity> GetListDataByCondition(int city, int cat, int rank)
        {
            /*  if (city > 0 && cat > 0 && rank > 0)
                  return this.CreateQuery()
                 .Where(o => o.DeleteFlg == 0 && o.City == city && o.Catalogue == cat && o.Price == rank).ToList();
              */
            return this.CreateQuery()
               .Where(o => o.DeleteFlg == 0).OrderBy(true, "ID DESC").ToList();
        }
        public List<MMapLocationEntity> GetListDataByPoint(decimal x1, decimal y1, decimal x2, decimal y2, int city, int cat, int rank)
        {
            //return this.CreateQuery().Where(o => decimal.Parse(o.Lattitude) >= x1 & decimal.Parse(o.Lattitude) < x2 &&
            // decimal.Parse(o.Longtitude) >= y1 && decimal.Parse(o.Longtitude) <= y2).ToList();
            List<MMapLocationEntity> lst = this.CreateQuery().Where(o => o.DeleteFlg == 0).ToList();
            //city alway >0



            if (city > 0 && cat > 0 && rank > 0)
            {
                return lst.Where(l => decimal.Parse(l.Longtitude) >= x1 && decimal.Parse(l.Longtitude) < x2 &&
              decimal.Parse(l.Lattitude) >= y1 && decimal.Parse(l.Lattitude) <= y2 && l.City == city && l.Catalogue == cat && l.Price == rank).ToList();
            }

            if (city > 0 && cat > 0 && rank == 0)
            {
                return lst.Where(l => decimal.Parse(l.Longtitude) >= x1 && decimal.Parse(l.Longtitude) < x2 &&
                decimal.Parse(l.Lattitude) >= y1 && decimal.Parse(l.Lattitude) <= y2 && l.City == city && l.Catalogue == cat).ToList();
            }

            if (city > 0 && cat == 0 && rank > 0)
            {
                return lst.Where(l => decimal.Parse(l.Longtitude) >= x1 && decimal.Parse(l.Longtitude) < x2 &&
             decimal.Parse(l.Lattitude) >= y1 && decimal.Parse(l.Lattitude) <= y2 && l.City == city && l.Price == rank).ToList();
            }

            if (city == 0 && cat > 0 && rank > 0)
            {
                return lst.Where(l => decimal.Parse(l.Longtitude) >= x1 && decimal.Parse(l.Longtitude) < x2 &&
             decimal.Parse(l.Lattitude) >= y1 && decimal.Parse(l.Lattitude) <= y2 && l.Catalogue == cat && l.Price == rank).ToList();
            }

            return lst.Where(l => decimal.Parse(l.Longtitude) >= x1 && decimal.Parse(l.Longtitude) < x2 &&
         decimal.Parse(l.Lattitude) >= y1 && decimal.Parse(l.Lattitude) <= y2 && l.City == city).ToList();


        }
    }

    public class MMapLocationModel : DefaultModel
    {
        [DataInfo]
        public int Id { get; set; }

        [DataInfo]
        public int MapId { get; set; }

        [DataInfo]
        public int LangID { get; set; }

        [DataInfo]
        public int Catalogue { get; set; }

        [DataInfo]
        public int City { get; set; }

        [DataInfo]
        public int Ward { get; set; }
        [DataInfo]
        public decimal PriceValue { get; set; }


        [DataInfo]
        public String Longtitude { get; set; }

        [DataInfo]
        public String Lattitude { get; set; }

        [DataInfo]
        public int Tittle { get; set; }

        [DataInfo]
        public int Content { get; set; }

        [DataInfo]
        public int Intro { get; set; }

        [DataInfo]
        public int ImgUrl { get; set; }

        [DataInfo]
        public int DeleteFlg { get; set; }

        [DataInfo]
        public String Address { get; set; }






    }
}
